﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using com.iss.inventory.Entity;
using com.iss.inventory.BusinessLogic;
using com.iss.inventory.CustomModels;
using com.iss.inventory.Controllers;

namespace com.iss.inventory.Controllers
{
    public class ApproveAdjVoucherController
    {
        public ApproveAdjVoucherController() { }

        public List<VoucherDetailsModel> getAllPendingVouchers(String Role){
            InvManagementEntities IME=new InvManagementEntities();
            List<VoucherDetailsModel> allPendingVouchers=new List<VoucherDetailsModel>();
            var pendingVouchers=from voucher in IME.AdjVoucherDetails
                                where voucher.AdjustID==null
                                select voucher;
            foreach(AdjVoucherDetail pendingVoucher in pendingVouchers ){
                VoucherDetailsModel pvdm=new VoucherDetailsModel();
                pvdm.AdjVoucherDetailID = (int)pendingVoucher.AdjVoucherDetailsID;pvdm.ItemID=(int)pendingVoucher.ItemID;pvdm.Reason=(String)pendingVoucher.Reason;
                int qtyAdj=(int)pendingVoucher.QtyAdjust;
                SupplierPriceList sp = IME.SupplierPriceLists.First<SupplierPriceList>(x => x.ItemID == pvdm.ItemID);
                if(qtyAdj>=0){
                    pvdm.QtyAdj="+"+Convert.ToString(qtyAdj);
                    pvdm.Cost=0;
                }else{
                    pvdm.QtyAdj=Convert.ToString(qtyAdj);
                    pvdm.Cost=(double)(qtyAdj*sp.SupplierPrice);
                }
                if (Role == "Supervisor") {
                    if (pvdm.Cost > -250) allPendingVouchers.Add(pvdm);
                }
                else if (Role == "Head") {
                    if (pvdm.Cost < -250) allPendingVouchers.Add(pvdm);
                }
            }
            return allPendingVouchers;
        }

        public String deletePendingVoucher(int adjVoucherDetailID){
            InvManagementEntities IME=new InvManagementEntities();
            AdjVoucherDetail avd = IME.AdjVoucherDetails.First<AdjVoucherDetail>(x => x.AdjVoucherDetailsID == adjVoucherDetailID);
            IME.DeleteObject(avd);
            IME.SaveChanges();
            return "Adjustment Voucher Delete Successfully";
        }

        public String approvePendingVoucher(List<int> voucherList, String empName, DateTime date)
        {
            InvManagementEntities IME = new InvManagementEntities();
            AdjVoucher av = new AdjVoucher();
            Employee e = IME.Employees.First<Employee>(x => x.Name == empName);
            av.ApprovalbyID = e.EmployeeID; av.DateIssued = date;
            IME.AddToAdjVouchers(av); IME.SaveChanges();
            AdjVoucher aV = (from s in IME.AdjVouchers where s.VoucherID == null select s).First();

            aV.VoucherID = aV.AdjustID - 909900; IME.SaveChanges();
            foreach (int voucherDetailID in voucherList) {
                AdjVoucherDetail avd = IME.AdjVoucherDetails.First<AdjVoucherDetail>(x => x.AdjVoucherDetailsID == voucherDetailID);
                avd.AdjustID = aV.AdjustID; IME.SaveChanges();
            }
            return "Generated Voucher No "+ aV.AdjustID;
        }
    }
}
